package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientBidcompetitor;
import com.woniu.cms.client.core.entity.ClientCompetitor;
import com.woniu.cms.client.core.service.IClientBidcompetitorService;
import com.woniu.cms.client.core.service.IClientCompetitorService;
import com.woniu.cms.client.core.vo.ClientBidcompetitorVo;
import com.woniu.cms.client.core.vo.ClientCompetitorVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 本表是记录每个竞争对手在每次投标中的表现，注意和cms_client_competitor，竞争对手基本信息表区分开 前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/client-bidcompetitor")
public class ClientBidcompetitorController {

    @Autowired
    private IClientBidcompetitorService iClientBidcompetitorService;

    @ApiOperation("根据id删除竞争对手表现表中的信息")
    @DeleteMapping("/{id}")
    public Result deleteClientBidcompetitor(@PathVariable Integer id){

        boolean b = iClientBidcompetitorService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除竞争对手表现表中的信息")
    @DeleteMapping
    public Result deleteClientBidcompetitor(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iClientBidcompetitorService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    @ApiOperation("修改竞争对手表现表中的信息")
    @PutMapping
    public Result updateClientBidcompetitor(@RequestBody ClientBidcompetitor clientBidcompetitor){

        boolean save = iClientBidcompetitorService.updateById(clientBidcompetitor);
        return save ? Result.ok() :Result.error();
    }

    @ApiOperation("保存竞争对手表现表中的信息")
    @PostMapping
    public Result saveClientBidcompetitor(@RequestBody ClientBidcompetitor clientBidcompetitor){

        boolean save = iClientBidcompetitorService.save(clientBidcompetitor);

        return save ? Result.ok() :Result.error();
    }



    @ApiOperation(value = "通过竞争对手表现表中的信息表Id查找竞争对手表现表中的信息对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        ClientBidcompetitor clientBidcompetitor = iClientBidcompetitorService.getById(id);

        return Result.common(clientBidcompetitor);
    }


    @GetMapping("page")
    @ApiOperation(value = "竞争对手表现表中的信息分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientBidcompetitor(Integer currentPage, Integer pageSize){

        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);


        IPage<ClientBidcompetitor> clientBidcompetitorIPage = new Page(currentPage,pageSize);

        IPage<ClientBidcompetitor> page = iClientBidcompetitorService.page(clientBidcompetitorIPage);

        System.out.println("page = " + page);

        List<ClientBidcompetitor> records = page.getRecords();

        System.out.println("records = " + records);

        long total = page.getTotal();

        System.out.println("total = " + total);

        List<ClientBidcompetitorVo> clientBidcompetitorVos = copyListProperties(records);

        Map<String,Object> map = new HashMap<>();
        map.put("data",clientBidcompetitorVos);
        map.put("total",total);

        return Result.common(map);


    }


    private List<ClientBidcompetitorVo>  copyListProperties(List<ClientBidcompetitor>  records){

        List<ClientBidcompetitorVo> clientBidcompetitorVos = MyBeanUtils.copyListProperties(records, ClientBidcompetitorVo::new);

        return clientBidcompetitorVos;

    }





}

